using Exploder.Utils;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Exploder.Demo
{
	public class DemoClickExplode : MonoBehaviour
	{
		private GameObject[] DestroyableObjects;

		private ExploderObject Exploder;

		public Camera Camera;

		private void Start()
		{
			Application.targetFrameRate = 60;
			Exploder = ExploderSingleton.ExploderInstance;
			if (Exploder.DontUseTag)
			{
				Object[] array = UnityEngine.Object.FindObjectsOfType(typeof(Explodable));
				List<GameObject> list = new List<GameObject>(array.Length);
				list.AddRange(from Explodable ex in array
					where ex
					select ex.gameObject);
				DestroyableObjects = list.ToArray();
			}
			else
			{
				DestroyableObjects = GameObject.FindGameObjectsWithTag("Exploder");
			}
		}

		private bool IsExplodable(GameObject obj)
		{
			if (Exploder.DontUseTag)
			{
				return obj.GetComponent<Explodable>() != null;
			}
			return obj.CompareTag(ExploderObject.Tag);
		}

		private void Update()
		{
			if (!Input.GetMouseButtonDown(0) && !Input.GetMouseButtonDown(1))
			{
				return;
			}
			Ray ray = (!Camera) ? Camera.main.ScreenPointToRay(UnityEngine.Input.mousePosition) : Camera.ScreenPointToRay(UnityEngine.Input.mousePosition);
			if (!Physics.Raycast(ray, out RaycastHit hitInfo))
			{
				return;
			}
			GameObject gameObject = hitInfo.collider.gameObject;
			if (IsExplodable(gameObject))
			{
				if (Input.GetMouseButtonDown(0))
				{
					ExplodeObject(gameObject);
				}
				else
				{
					ExplodeAfterCrack();
				}
			}
		}

		private void ExplodeObject(GameObject obj)
		{
			Exploder.ExplodeObject(obj, OnExplosion);
		}

		private void OnExplosion(float time, ExploderObject.ExplosionState state)
		{
			if (state != ExploderObject.ExplosionState.ExplosionFinished)
			{
			}
		}

		private void OnCracked()
		{
		}

		private void ExplodeAfterCrack()
		{
		}

		private void OnGUI()
		{
			if (GUI.Button(new Rect(10f, 10f, 100f, 30f), "Reset") && !Exploder.DestroyOriginalObject)
			{
				GameObject[] destroyableObjects = DestroyableObjects;
				foreach (GameObject obj in destroyableObjects)
				{
					ExploderUtils.SetActiveRecursively(obj, status: true);
				}
				ExploderUtils.SetActive(Exploder.gameObject, status: true);
			}
		}
	}
}
